package com.example.koizzi.ad20180606_note;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

public class MyContentProvider extends ContentProvider {
    public static final String URI_STUDENT = "content://com.xipai.studb/student";
    private static final String AUTHORITY = "com.xipai.studb";
    static UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        sMatcher.addURI(AUTHORITY, StudentTable.TABLE_NAME, 1);
//        sMatcher.addURI(AUTHORITY,"score",2);
    }
    private MyOpenHelper myOpenHelper;
    @Override
    public boolean onCreate() {
        myOpenHelper = new MyOpenHelper(getContext(), "stu.db", null, 1);
        Log.e("provider","provider");
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
        switch (sMatcher.match(uri)) {
            case 1:
                SQLiteDatabase database1 = myOpenHelper.getReadableDatabase();
                Cursor cursor = database1.query(StudentTable.TABLE_NAME, strings, s, strings1, null, null, s1);
//              database1.close();
                return cursor;
        }
        return null;

    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        switch (sMatcher.match(uri)) {
            case 1:
                SQLiteDatabase database1 = myOpenHelper.getWritableDatabase();
                database1.insert(StudentTable.TABLE_NAME, null, contentValues);
                database1.close();
                break;
//            case 2:
//                SQLiteDatabase database2 = myOpenHelper.getReadableDatabase();
//                database2.insert("score",null,contentValues);
//                database2.close();
//                break;
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        int deletedRows = 0;
        switch (sMatcher.match(uri)) {
            case 1:
                SQLiteDatabase database1 = myOpenHelper.getWritableDatabase();
                deletedRows = database1.delete(StudentTable.TABLE_NAME, s, strings);
                break;
        }

        return deletedRows;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        int updateRows = 0;
        switch (sMatcher.match(uri)) {
            case 1:
                SQLiteDatabase database1 = myOpenHelper.getWritableDatabase();
                updateRows = database1.update(StudentTable.TABLE_NAME, contentValues, s, strings);
        }
        return updateRows;
    }
}

